package com.base.cn.platform.os.service.order.order;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.order.ShopType;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.dao.order.order.ShopCartDao;
import com.base.cn.platform.os.entity.order.order.ShopCart;
import com.base.cn.platform.os.entity.order.shop.ShopData;
import com.base.cn.platform.os.service.order.init.ShopDataInit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 购物车Biz
 *
 * @author s.li
 * @create 2018-05-09-14:54
 */
@Service
public class ShopCartBiz extends BaseBiz<ShopCart,ShopCartDao> {

    @Autowired
    private ShopDataInit shopDataInit;

    /**
     * 查询用户购物车列表
     * @param userId 用户ID
     * @return 购物车列表
     */
    public List<ShopCart> findUserShopCartList(BigDecimal userId){
        List<ShopCart> shopCartList = this.find(" userId="+userId,null,null);
        return shopCartList;
    }

    /**
     * 删除订单创建后的购物车商品
     * @param shopCartIds
     */
    @Async
    public void deleteOrderShopCart(String shopCartIds){
        this.deleteShopCartIds(shopCartIds);
    }

    /**
     * 删除购物车
     * @param shopCartIds 购物车ID串
     */
    public void deleteShopCartIds(String shopCartIds){
        shopCartIds = StringUtils.subHeadTailString(shopCartIds,",");
        if(StringUtils.isNotEmpty(shopCartIds)){
            this.deleteWhereSql(" id in ("+shopCartIds+")");
        }
    }

    /**
     * 添加购物车数据
     * @param shopCart 购物车对象
     */
    public Map<String, Object> addShopCart(ShopCart shopCart){
        ShopCart _shopCart = this.findOne(" userId="+shopCart.getUserId()+" and shopId="+shopCart.getShopId()+" and shopType='"+shopCart.getShopType()+"'",null);
        if(ObjectUtils.isNotEmpty(_shopCart)){//购物车中已存在该商品
            if(ShopType.ORDER_SHOP_BOOK.equals(shopCart.getShopType())){//图书商品
                int buyNum = _shopCart.getBuyNum()+shopCart.getBuyNum();
                _shopCart.setBuyNum(buyNum);
                this.updateById(_shopCart,null);
            }
            return ResultUtil.ERROR("商品已存在，请勿重复添加");
        }else{//购物车中没有存在该商品
            this.save(shopCart);
            return ResultUtil.SUCCESS("添加购物车成功");
        }
    }

    /**
     * 查询前台显示的用户购买车的商品数据
     * @param userId 用户ID
     * @return List<Shop>
     */
    public List<ShopCart> findWebShowShopCartList(BigDecimal userId){
        List<ShopCart> shopCartList = this.findUserShopCartList(userId);
        if(ObjectUtils.isNotEmpty(shopCartList)){
            List<ShopData> shopDataList = new ArrayList<>();
            shopCartList.forEach(shopCart -> {
                ShopData shopData = new ShopData();
                shopData.setShopId(shopCart.getShopId());
                shopData.setShopType(shopCart.getShopType());
                shopData.setShopCartId(shopCart.getId());
                shopData.setBuyNum(shopCart.getBuyNum());
                shopDataList.add(shopData);
            });
            shopDataInit.setShopData(shopDataList);

            Map<String,ShopCart> shopCartMap = shopCartList.stream().collect(Collectors.toMap(e->e.getShopType()+e.getShopId(),e->e));
            Map<String,ShopData> shopDataMap = shopDataList.stream().collect(Collectors.toMap(e->e.getShopType()+e.getShopId(),e->e));
            shopCartMap.forEach((k,v)->{
                ShopData shopData = shopDataMap.get(k);
                v.setShop(shopData);
            });
        }
        return shopCartList;
    }

    /**
     * 查询用户购物车商品数量
     * @param userId 用户id
     * @return 购物车中商品数量
     */
    public Integer queryCartShopNum(BigDecimal userId){
        //暂时未加缓存，有需要，后需添加
        return this.count(" userId = "+userId).intValue();
    }
}
